A conditional statement in vbscript is used to perform different task or action which base on a specified condition is true or false. an if else is conditional statement which execute task or action if condition is true otherwise else condition is executed. you can use multiple if else statement within vbscript. The condition of if else statement is an expression with Boolean value (TRUE or FALSE). Logical expression or comparative expressions can be stated as conditions. When the if else condition is executed, the stated expressions are evaluated. If the value of an expression is TRUE, the if task is executed otherwise else task is executed.
if then conditional statement execute task if condition of if statement is true. following are the expression of If then conditional statement in wincc hmi vbscript
If _condition_ Then
'statements
End If
above is the example of if then conditional statement in wincc vbscript. in this example we have two input output field which associated with tag1 and tag2 and when button pressed events evaluates if condition. If the condition is true then statement executed. Condition for this example is to evaluate value of both input output fields if the value are equal than textfild1 text changed to both values are equal. following are code explanation
if then else statement in VBScript is used to perform conditional checks and execute specific blocks of code based on the result of the condition. if the condition is true then if statement is executed if the condition is false then else condition is executed following are the expression of if then else conditional statement in wincc vbscript
If _condition Then
' Code to execute if the condition is true
Else
' Code to execute if the condition is false
End If
below is the example of if then else conditional statement in wincc vbscript. in this example compare two input output fields value when button is pressed if the values are equal then text field text changed to "values are equal otherwise else statement is executed and text field text changed to "both values are not equal". tag1 and tag2 are associated with both input output field.
following are the code explanation
result
variable to reference a text field object on the screen named "screen1".tag1
.tag2
.tag1
and tag2
are equal.tag1
and tag2
) are retrieved and stored in variables."Both Values Are Equal"
."Both Values Are Not Equal"
.A multiple If-Else statement evaluates multiple conditions, and if any of the conditions are true, it executes the corresponding block of code. If none of the conditions are true, the Else block is executed. This type of statement is useful when you need to check several conditions based on a single variable or expression and perform different actions depending on the outcome of each condition. It works by sequentially checking each condition. Once a true condition is found, the associated block of code is executed, and the remaining conditions are skipped. If no condition evaluates to true, the Else block is executed as a fallback. following are the expression of multiple if else statement in wincc vbscript
If _condition1 Then
' Executes when condition1 is true
ElseIf _condition2 Then
' Executes when condition1 is false, but condition2 is true
ElseIf _condition3 Then
'Executes when both condition1 and condition2 are false, but condition3 is true
Else
' Executes when none of the above conditions are true
End If
below is the multiple if else statement in wincc vbscript. in this script compare two value. comperison result depend on condition. value1 and value 2 is associated with tag 1 and tag 2. tag 1 and tag2 are tag for input output fields. condition is executed when button is pressed.
The script compares two values and uses conditional statements (If-Else
) to execute different blocks of code based on their relationship. Below is a breakdown of the code:
value1
and value2
: The script retrieves values from two tags, tag1
and tag2
, using the SmartTags
function. These values will be compared.If value1 = value2
): If value1
is equal to value2
, the message "Both values are equal" is displayed on the screen.ElseIf value1 > value2
): If the first condition is false, the script checks if value1
is greater than value2
. If true, the message "Input 1 is greater than Input 2" is displayed.ElseIf value1 < value2
): If neither of the first two conditions are true, the script checks if value1
is less than value2
. If true, the message "Input 1 is less than Input 2" is shown.Else
): If none of the above conditions are true (although unlikely in this case), the Else
block will be executed, displaying "Unexpected case". This ensures that there is always some feedback displayed.